const connection = require("../app/database");
const { to } = require("../utils");

class PermissionService {
    async checkMoment(momentId, id) {
        const statement = `SELECT * FROM moment WHERE id = ? AND user_id = ?`;
        const [error, resp] = await to(connection.execute(statement, [momentId, id]));
        if (error) {
            return Promise.reject(error);
        }
        const [result] = resp;
        return !!result.length;
    }

    async checkPermission(tableName, tableId, userId) {
        const statement = `SELECT * FROM ${tableName} WHERE id = ? AND user_id = ?`;
        const [error, resp] = await to(connection.execute(statement, [tableId, userId]));
        if (error) {
            return Promise.reject(error);
        }
        const [result] = resp;
        return !!result.length;
    }
}

module.exports = new PermissionService();
